
#include <lvgl.h>
#include <string.h>
//#include "sdcard_file.h"
#include "mjpeg_frame.h"
#include "img_converters.h"
#include "esp_log.h"
#include "sd_card/SD_MMC.h"

#include "sys/stat.h"
#include <fcntl.h>
#include "video_play.h"
#include "jpeg_decoder.h"

#define TAG     "pic_jpeg_decode"

char *file_source_buf  = NULL;
uint8_t *file_buffer = NULL;

// 转换RGB565字节顺序的函数
void convert_rgb565_endianness(uint16_t *data, size_t size) {
    for (size_t i = 0; i < size; i++) {
        data[i] = (data[i] << 8) | (data[i] >> 8);
    }
}


/*
读取图片并显示
file_name /sdcard/pic4.jpg
*/
int led_pic_display(lv_obj_t* obj_pic,char*file_name)
{
    struct stat st;
    const char *filename = file_name;                                                            /*!< filename */
    uint32_t filesize = 0;
    if (stat(filename, &st) == 0)
    {
        ESP_LOGI(TAG, "File %s size is %ld\n", filename, st.st_size);
        filesize = (uint32_t)st.st_size;                                                             /*!< read image file size */

        if (file_source_buf == NULL)
        {
            //file_source_buf = heap_caps_malloc(filesize + 1, MALLOC_CAP_DMA);
            file_source_buf = malloc(filesize + 1);
            
        }
        else{
            memset(file_source_buf,0,filesize + 1);
        }
    }
    else
    {
        ESP_LOGI(TAG,"Read %s size fail",filename);
        return -1;
    }
   // uint8_t *file_buffer = NULL; /*!< decode image buffer */
    size_t file_buffer_size = 0; /*!< decode image buffer size */

    int f = open(filename, O_RDONLY);
    if (f > 0)
    {
        int readbyte = read(f, file_source_buf, filesize);
        close(f);

        ESP_LOGI(TAG, "Decode jpg");
        file_buffer_size = 1280 * 960 * sizeof(uint16_t);
        if(!file_buffer)
        {
            file_buffer = malloc(file_buffer_size);                          /*!< create out image buffer */
            memset(file_buffer,0,file_buffer_size);
        }
        else{
             memset(file_buffer,0,file_buffer_size);
        }
        esp_jpeg_image_cfg_t jpeg_cfg = {
            .indata = (uint8_t *)file_source_buf,
            .indata_size = filesize,
            .outbuf = file_buffer,
            .outbuf_size = file_buffer_size,
            .out_format = JPEG_IMAGE_FORMAT_RGB565,
            .out_scale = JPEG_IMAGE_SCALE_1_4,
            .flags = {
                .swap_color_bytes = 1,
            },
        };
        esp_jpeg_image_output_t outimage;
        esp_jpeg_decode(&jpeg_cfg, &outimage);
        ESP_LOGI(TAG, "%s size: %d x %d", filename, outimage.width, outimage.height);

        // 转换RGB565字节顺序的函数
        convert_rgb565_endianness(file_buffer,outimage.output_len);

        uint8_t *aligned_data = (uint8_t*)(((uint32_t)file_buffer + 3) & ~0x03);
        static lv_img_dsc_t img_dsc;
        memset(&img_dsc,0,sizeof(lv_img_dsc_t));
        img_dsc.header.cf = LV_COLOR_FORMAT_RGB565;
        img_dsc.data = aligned_data;
        img_dsc.data_size = (uint32_t)outimage.width*(uint32_t)outimage.height*2;
        img_dsc.header.h = outimage.height;
        img_dsc.header.w = outimage.width;
        //ESP_LOGI(TAG,"rgb img,w:%d,h:%d,size:%d",height,width,(size_t)img_dsc.data_size);
        lv_img_set_src(obj_pic,&img_dsc);
        lv_refr_now(NULL);
    }
    else
    {
        ESP_LOGI(TAG, "open pic failed\n");
        return -1;
    }
    return 0;
}